package com.koron.configure.workflow.bean.enums;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.koron.bean.system.org.OrgBean;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.business.system.SystemCacheUtil;
import com.koron.common.core.util.OrgCacheUtils;
import com.koron.configure.workflow.service.impl.StaffHelper;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

/**
 * 一些部门标识
 * @author jzp
 * @since 2022/6/17
 */
@AllArgsConstructor
@Getter
public enum DeptMarkEnum {

    /**
     * 空部门
     */
    NA("na", formVariables -> new ArrayList<>()),

    /**
     * 业务数据的deptId字段对应的部门
     */
    DEPT_ID("deptId", formVariables -> {
        JSONObject jsonObject = JSONObject.parseObject(formVariables);
        String deptId = jsonObject.getString("deptId");
        WorkflowUser workflowUser = new WorkflowUser(deptId, null, 1);
        return Collections.singletonList(workflowUser);
    }),

    /**
     * 创建人所在单位
     */
    CREATE_BY_ORG("createByOrg", formVariables -> {
        JSONObject jsonObject = JSONObject.parseObject(formVariables);
        String createBy = jsonObject.getString("createBy");
        StaffHelper staffHelper = SpringUtil.getBean(StaffHelper.class);
        return staffHelper.getOrgByAccount(createBy);
    }),

    /**
     * 创建人所在部门
     */
    CREATE_BY_DEPT("createByDept", formVariables -> {
        JSONObject jsonObject = JSONObject.parseObject(formVariables);
        String createBy = jsonObject.getString("createBy");
        StaffHelper staffHelper = SpringUtil.getBean(StaffHelper.class);
        return staffHelper.getDeptByAccount(createBy);
    })
    ;
    private String mark;
    private Function<String, List<WorkflowUser>> function;

    public static Function<String, List<WorkflowUser>> getByMark(String mark) {
        for (DeptMarkEnum value : values()) {
            if (Objects.equals(value.getMark(), mark)) {
                return value.getFunction();
            }
        }
        // 如果是$开头，则为一个变量
        if (mark.startsWith("$")) {
            // 如果只是一个变量，则认为是一个部门，否则有冒号标识带表达式
            String variables = mark.substring(1);
            if (variables.contains(":")) {
                /*
                 * 如果有冒号,则认为是用户，然后取其单位或部门
                 * 例如$createBy:ORG 表示用户变量createBy的单位
                 */
                String[] split = variables.split(":");
                return formVariables -> {
                    JSONObject jsonObject = JSONObject.parseObject(formVariables);
                    String variable = jsonObject.getString(split[0]);
                    if ("DEPT".equals(split[1])) {
                        return SpringUtil.getBean(StaffHelper.class).getDeptByAccount(variable);
                    } else if ("ORG".equals(split[1])) {
                        return SpringUtil.getBean(StaffHelper.class).getOrgByAccount(variable);
                    }
                    // 暂时没有其他的表达式
                    throw new IllegalArgumentException("找不到表达式：" + split[1]);
                };
            } else {
                /*
                 * 如果没有表达式，则认为是一个部门，部门id和部门名称都用同一个值，如果后缀带个&，表示单位的部门
                 * 如$deptManage表示设备管理单位
                 * 如$deptManage&表示设备管理单位所在部门
                 * 如$deptManage^表示设备管理单位的上一级单位
                 */
                return formVariables -> {
                    JSONObject jsonObject = JSONObject.parseObject(formVariables);
                    if (variables.endsWith("&")) {
                        String deptId = jsonObject.getString(variables.substring(0, variables.length() - 1));
                        String deptName = deptId;
                        OrgBean orgBean = SystemCacheUtil.queryDeptByOrgId(deptId);
                        if (orgBean != null) {
                            deptName = orgBean.getName();
                            deptId = orgBean.getId();
                        }
                        return new ArrayList<>(Collections.singletonList(new WorkflowUser(deptName, deptId, 1)));
                    } else if (variables.endsWith("^")) {
                        String deptId = jsonObject.getString(variables.substring(0, variables.length() - 1));
                        String deptName = deptId;
                        OrgBean orgBean = SystemCacheUtil.queryUpByOrgId(deptId);
                        if (orgBean != null) {
                            deptName = orgBean.getName();
                            deptId = orgBean.getId();
                        }
                        return new ArrayList<>(Collections.singletonList(new WorkflowUser(deptName, deptId, 1)));
                    } else {
                        String deptId = jsonObject.getString(variables);
                        String deptName = deptId;
                        OrgBean orgBean = OrgCacheUtils.queryDeptByOrgId(deptId);
                        if (orgBean != null) {
                            deptName = orgBean.getName();
                            deptId = orgBean.getId();
                        }
                        return new ArrayList<>(Collections.singletonList(new WorkflowUser(deptName, deptId, 1)));
                    }
                };
            }
        }
        return formVariables -> null;
    }
}
